/// @tags: MonoStack DataStructure
#include <cstdio>
#include <functional>
#include <iostream>
#include <numeric>
#include <stack>

using std::cin, std::cout, std::endl;

namespace BlueQuantum {

constexpr int maxn = 1e6 + 2, mod = 1e9 + 7;

int n;
int a[maxn];
long long sum[maxn], val[maxn];

int lowbit(int x) { return x & -x; }

long long query(int x) {
  x += 1;
  long long res = 0;
  while (x) {
    res += val[x];
    if (res >= mod) res -= mod;
    x -= lowbit(x);
  }
  return res;
}

void modify(int x, long long v) {
  x += 1;
  while (x <= n + 2) {
    val[x] += v;
    if (val[x] >= mod) val[x] -= mod;
    if (val[x] < 0) val[x] += mod;
    x += lowbit(x);
  }
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    sum[i] = a[i] + sum[i - 1];
    if (sum[i] >= mod) sum[i] -= mod;
    if (sum[i] < 0) sum[i] += mod;
  }
  std::stack<int> stk;
  std::vector<int> l(n + 1), r(n + 1);
  for (int i = 1; i <= n; ++i) {
    while (!stk.empty() && a[i] > a[stk.top()]) {
      r[stk.top()] = i - 1;
      stk.pop();
    }
    stk.push(i);
  }
  while (!stk.empty()) {
    r[stk.top()] = n;
    stk.pop();
  }

  for (int i = n; i >= 1; --i) {
    while (!stk.empty() && a[i] > a[stk.top()]) {
      l[stk.top()] = i + 1;
      stk.pop();
    }
    stk.push(i);
  }
  while (!stk.empty()) {
    l[stk.top()] = 1;
    stk.pop();
  }

  for (int i = 1; i <= n; ++i) {
    modify(i, (i - l[i] + 1));
    modify(r[i] + 1, -(i - l[i] + 1));
    modify(l[i] - 1, -(r[i] - i + 1));
    modify(i, (r[i] - i + 1));
  }
  long long ans = 0;
  for (int i = 0; i <= n; ++i) {
    ans += sum[i] * query(i) % mod;
    ans %= mod;
    if (ans < 0) ans += mod;
  }
  cout << ans;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("I. The last problem.in", "r", stdin);
  freopen("I. The last problem.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
